package mo.leetcode_top;

import java.util.Arrays;

public class L34 {

    public int[] searchRange(int[] nums, int target) {
        int left = 0;
        int right = nums.length;
        int firstIndex = -1;
        int lastIndex = -1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        if (right == nums.length || nums[right] != target) {
            return new int[]{firstIndex, lastIndex};
        }
        firstIndex = right;
        right = nums.length;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] <= target) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        lastIndex = right - 1;
        return new int[]{firstIndex, lastIndex};
    }

    public static void main(String[] args) {
        L34 test = new L34();
        System.out.println(Arrays.toString(test.searchRange(new int[]{1}, 1)));
    }
}
